<template>
	<view>
		<view class="head_warp">
			<view class="head_image">
				<image :src="userInfo.wxHeadUrl" mode="aspectFit"></image>
			</view>
			<view class="user_name">
				{{userInfo.wxUserName}}
			</view>
		</view>

		<view class="list_warp">
			<u-cell-group>
				<u-cell-item title="订单中心" border-bottom :arrow="true" arrow-direction="right"></u-cell-item>
				<u-cell-item @click="toAddressManager" title="地址管理" border-bottom :arrow="true" arrow-direction="right"></u-cell-item>
				<u-cell-item @click="linkToBusinessCooperation" title="商务合作" border-bottom :arrow="true" arrow-direction="right"></u-cell-item>
			</u-cell-group>
		</view>
		
		<u-popup v-model="noUserInfo" mode="center" border-radius="14">
				<view>
					为了更好提供服务，需要获取您的微信信息
				</view>
				<u-button open-type="getUserInfo" @click="getWxUserInfo">同意</u-button>
			</u-popup>
		
	</view>

</template>

<script>
	import {
		baseUrl
	} from '@/api/base'
	import {
		initOpenId,
		updateUserInfo
	} from '@/api/init'
	export default {
		data() {
			return {
				userInfo: getApp().globalData.userInfo,
				noUserInfo: false,
			}
		},
		onShow() {
			this.getUserInfo();
		},
		methods: {
			linkToBusinessCooperation() {
				uni.redirectTo({
					url: '/pages/businessCooperation/index'
				})
			},
			toAddressManager() {
				uni.redirectTo({
					url: "/pages/addressManager/index"
				})
			},
			getUserInfo() {
				let openId = getApp().globalData.openId;
				if (!openId) {
					setTimeout(() => {
						initOpenId();
						this.getUserInfo()
					}, 1000);
				}
				if (openId) {
					let userInfo = getApp().globalData.userInfo;
					if (!userInfo.nickName) {
						this.request({
							url: baseUrl + '/mini/user/getSignUserByOpenId',
							method: 'post',
							data: {
								openId: openId
							}
						}).then(resp => {
							if(!resp.data.hasError){
								let nickName = resp.data.result.wxUserName;
								if(!nickName){
									this.noUserInfo = true;
								}else{
								getApp().globalData.userInfo = resp.data.result;
								this.userInfo = resp.data.result;
								}
							}
						})
					}else{
						this.userInfo = userInfo;
					}
				}

			},
			getWxUserInfo(){
				uni.getUserInfo({
					success:res=>{
						const userInfo = res.userInfo;
						updateUserInfo(userInfo);
					},
					complete: () => {
						this.getUserInfo();
					}
				})
				
			}
		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin: 200rpx auto 50rpx auto;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}

	.head_warp {
		display: flex;
		flex-direction: column;
		height: 170rpx;
		align-items: center;
	}

	.head_image {
		margin-top: 10rpx;
		margin-bottom: 10rpx;
	}

	.head_image image {
		height: 120rpx;
		width: 120rpx;
		border-radius: 50%;
	}

	.user_name {
		font-size: 14px;
		font-weight: bold;
	}

	.list_warp {
		margin-top: 20rpx;
	}
</style>
